/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;

using DotNetNuke.Modules.Articles.Framework;
using DotNetNuke.Modules.Articles.Common;
using DotNetNuke.ModuleToolkit.Modules.Skinning.SkinObjects;
using DotNetNuke.ModuleToolkit.Common;

#endregion

namespace DotNetNuke.Modules.Articles.UI.DataSources
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ArticlesDataSource skin object provides a DataSource for a list of
    /// Articles.
    /// </summary>
    /// <history>
    /// 	[cnurse]	01/18/2007	created
    /// </history>
    /// -----------------------------------------------------------------------------
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
    ToolboxData("<{0}:ArticlesDataSource runat=\"server\"> </{0}:ArticlesDataSource>")]
    public class ArticlesDataSource : BaseDataSource
    {

        #region Private Members

        private List<ArticleInfo> _Articles;
        private ListMode _Mode;

        #endregion

        #region Protected Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the Articles Module Base for this control
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/18/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected ArticlesModuleBase ParentArticlesModule
        {
            get { return (ArticlesModuleBase)base.ParentModule; }
        }

        #endregion

        #region Public Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the articles to display
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/18/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        public List<ArticleInfo> Articles
        {
            get
            {
                if (_Articles == null)
                {
                    _Articles = new List<ArticleInfo>();
                    int _TotalRecords = Null.NullInteger;

                    if (DesignMode)
                    {
                        ArticleInfo article;
                        for (int i = 0; i < 5; i++)
                        {
                            article = new ArticleInfo();
                            article.Title = "Article " + i.ToString();
                            _Articles.Add(article);
                        }
                    }
                    else
                    {
                        switch (Mode)
                        {
                            case ListMode.Section:
                                if (IsPaged)
                                {
                                    _Articles = ArticleController.GetArticlesBySectionByPage(ParentModule.ModuleId, ParentArticlesModule.SectionId, CurrentPage - 1, PageSize, ref _TotalRecords);

                                    //Override the ArticlesModuleBase Articles collection
                                    ParentArticlesModule.Articles = _Articles;
                                }
                                else
                                {
                                    _Articles = ParentArticlesModule.Articles;
                                    _TotalRecords = _Articles.Count;
                                }
                                break;
                            case ListMode.AllArticles:
                                if (IsPaged)
                                {
                                    _Articles = ArticleController.GetAllArticlesByPage(ParentModule.ModuleId, CurrentPage - 1, PageSize, ref _TotalRecords);
                                }
                                else
                                {
                                    _Articles = ArticleController.GetAllArticles(ParentModule.ModuleId);
                                    _TotalRecords = _Articles.Count;
                                }
                                //Override the ArticlesModuleBase Articles collection
                                ParentArticlesModule.Articles = _Articles;
                                break;
                            case ListMode.MyArticles:
                                if (IsPaged)
                                {
                                    _Articles = ArticleController.GetArticlesByCreatorAndStatusByPage(ParentModule.ModuleId, ParentArticlesModule.UserId, PublishedStatus.Any, CurrentPage - 1, PageSize, ref _TotalRecords);
                                }
                                else
                                {
                                    _Articles = ArticleController.GetArticlesByCreatorAndStatus(ParentModule.ModuleId, ParentArticlesModule.UserId, PublishedStatus.Any);
                                    _TotalRecords = _Articles.Count;
                                }
                                //Override the ArticlesModuleBase Articles collection
                                ParentArticlesModule.Articles = _Articles;
                                break;
                            case ListMode.Moderate:
                                if (IsPaged)
                                {
                                    _Articles = ArticleController.GetArticlesByCreatorAndStatusByPage(ParentModule.ModuleId, Null.NullInteger, PublishedStatus.Submitted, CurrentPage - 1, PageSize, ref _TotalRecords);
                                }
                                else
                                {
                                    _Articles = ArticleController.GetArticlesByCreatorAndStatus(ParentModule.ModuleId, Null.NullInteger, PublishedStatus.Submitted);
                                    _TotalRecords = _Articles.Count;
                                }
                                //Override the ArticlesModuleBase Articles collection
                                ParentArticlesModule.Articles = _Articles;
                                break;
                        }
                    }
                    TotalRecords = _TotalRecords;
                }
            return _Articles;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the List Mode
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/18/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Behavior"),
        DefaultValue(ListMode.AllArticles),
        Description("The Mode for the List")]
        public ListMode Mode
        {
            get { return _Mode; }
            set { _Mode = value; }
        }

        #endregion

    }
}
